#include "common.h"

#include "mainwindow.h"
#include "usertool.h"

#include <QtCore/QEvent>
#include <QtCore/QObject>
#include <QtGui/QMouseEvent>

void UserTool::installTool(class Viewer *viewer)
{
    viewer->installEventFilter(this);
}

void UserTool::uninstallTool(class Viewer *viewer)
{
    viewer->removeEventFilter(this);
}

ZoomTool::ZoomTool(double factor) : zoomFactor(factor)
{    
}

SearchTool::SearchTool ()
{    
}

bool ZoomTool::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonRelease)
    {    
        Viewer *viewer = qobject_cast<Viewer *>(object);
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

        if (viewer)
        {
            viewer->zoom(mouseEvent->pos(), zoomFactor);
        }

        return true;
    }

    return false;
}

bool DragTool::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {    
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        point = mouseEvent->pos();
        return true;
    }
    else if (event->type() == QEvent::MouseMove && !point.isNull())
    {
        Viewer *viewer = qobject_cast<Viewer *>(object);
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

        if (viewer)
        {
            viewer->scrollTo(point, mouseEvent->pos());
            point = mouseEvent->pos();
        }

        return true;
    }
    else if (event->type() == QEvent::MouseButtonRelease)
    {
        point = QPoint();
		return true;
    }

    return false;
}

bool SelectTool::eventFilter(QObject *object, QEvent *event)
{
	if (event->type() == QEvent::MouseButtonRelease)
	{
		Viewer *viewer = qobject_cast<Viewer *>(object);
		if (viewer)
		{
			QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
			viewer->pointSelectEvent(mouseEvent->pos());
			return true;
		}
	}

	return false;
}

bool SearchTool::eventFilter ( QObject *object, QEvent *event )
{
    return false;
}
